<template>
	<div class="bg">
		<div class="zzc" v-if="showzzc" @click="close"></div>
		<div class="container">
			<div class="search_box">
				<div class="select_box" @click.stop="options_show=!options_show;showzzc=!showzzc">
					<div class="form-control" id="select" style="width: 120px;">
						<div :style="ind=='all'?'color:#999':''">
							{{ind!='all'?types[ind].name:'查询方式'}}
						</div>
						<div v-if="ind!='all'" @click.stop="closeall">
							<img src="@assets/images/close.png" alt="">
						</div>
						<img src="@assets/images/down.png" alt="">
					</div>
					<div class="options" v-if="options_show">
						<!-- <div class="option" style="color: #999;">选择解决方式</div>
						<div class="option" @click="selectit2('all')">全部</div> -->
						<div class="option" @click.stop="selectit(index)" v-for="item,index in types">{{item.name}}
						</div>
					</div>
				</div>

				<el-date-picker v-if="ind==0||ind==2 || ind==1" prefix-icon='none' ref="year"
					style="width: 120px;margin-left: 10px;" v-model="info.year" type="year" @change="changedate(3)"
					value-format="yyyy" placeholder="年份">
				</el-date-picker>

				<div class="select_box" v-if="ind==2" @click.stop="options_show2=!options_show2;showzzc=!showzzc">
					<div class="form-control" id="select" style="width: 120px;">
						<div :style="ind2=='all'?'color:#999;':''">
							{{ind2!='all'?quarter[ind2]:'季度'}}
						</div>
						<div v-if="ind2!='all'" @click.stop="info.quarter='',ind2='all'">
							<img src="@assets/images/close.png" alt="">
						</div>

						<img src="@assets/images/down.png" alt="">
					</div>
					<div class="options" v-if="options_show2">
						<div class="option" @click.stop="selectit2(index)" v-for="item,index in quarter">{{item}}</div>
					</div>
				</div>
				<div class="select_box" v-if="ind==1" @click.stop="options_show3=!options_show3;showzzc=!showzzc">
					<div class="form-control" id="select" style="width: 120px;">
						<div :style="ind3=='all'?'color:#999;width:40px':'width:40px'">
							{{ind3!='all'?month[ind3]:'月'}}
						</div>
						<div v-if="ind3!='all'" @click.stop="info.month='',ind3='all'">
							<img src="@assets/images/close.png" alt="">
						</div>

						<img src="@assets/images/down.png" alt="">
					</div>
					<div class="options" v-if="options_show3">
						<div class="option" @click.stop="selectit3(index)" v-for="item,index in month">{{item}}</div>
					</div>
				</div>

				<el-date-picker v-if="ind==3" prefix-icon='none' style="width: 120px;margin-left: 10px;"
					v-model="startDate" type="date" @change="changedate(1)" value-format="timestamp" placeholder="开始日期">
				</el-date-picker>
				<el-date-picker v-if="ind==3" prefix-icon='none' style="width: 120px;margin-left: 10px;"
					v-model="endDate" type="date" @change="changedate(2)" value-format="timestamp" placeholder="结束日期">
				</el-date-picker>


				<div class="sea_btn sear_btns" @click="getlist">
					<img src="@assets/images/search.png" alt="">
					查询
				</div>

			</div>


			<div class="box">
				<div class="box_top">
					<div class="box_top_left" ref="container"></div>
					<div class="box_top_left" ref="timelong"></div>
				</div>
				<div class="table">
					<div class="table_top">
						<div class="table_title">工程师小组处理情况</div>
						<div class="top_btns">
							<div class="export" @click="export2Excel" v-if="$permissions.indexOf(85)  != -1">导出</div>
							<div class="print" @click="tabshow=true">打印</div>
						</div>
					</div>
					<el-table :data="tableData" style="width: 100%" :default-sort="{prop: 'name', order: 'descending'}">
						<el-table-column prop="name" label="小组" sortable>
						</el-table-column>
						<el-table-column prop="finish" label="完成" sortable>
						</el-table-column>
						<el-table-column prop="unFinish" label="未完成" sortable>
						</el-table-column>
						<el-table-column prop="deliver" label="转交" sortable>
						</el-table-column>
						<el-table-column prop="stop" label="终止" sortable>
						</el-table-column>
						<el-table-column prop="finishPersents" label="完成率" sortable>
						</el-table-column>
						<el-table-column prop="averageResponseTime" label="平均响应时长（小时）" sortable width="185">
						</el-table-column>
						<el-table-column prop="averageProcessingTime" label="平均处理时长（小时）" sortable width="185">
						</el-table-column>
						<el-table-column prop="averageWholeTime" label="平均总时长（小时）" sortable width="185">
						</el-table-column>
					</el-table>
				</div>
			</div>
			<div class="blackzzc" v-if="tabshow">
				<div class="tablealert" id="printDiv">
					<div class="top_btns">
						<div class="print" @click="print">确认打印</div>
						<div class="export" @click="tabshow=false">关闭</div>
					</div>
					<vueEasyPrint ref="easyPrint" tableShow>
						<div class="altabletitle">工程师小组处理情况</div>
						<table border="1" cellspacing='0'
							style="width:1000px;margin: 0 auto;text-align: center;color: #000;">
							<tr>
								<td>小组</td>
								<td>完成</td>
								<td>未完成</td>
								<td>转交</td>
								<td>终止</td>
								<td>完成率</td>
								<td>平均响应时长（小时）</td>
								<td>平均处理时长（小时）</td>
								<td>均总时长（小时）</td>
							</tr>
							<tr v-for="item in tableData">
								<td>{{item.groupName}}</td>
								<td>{{item.finish}}</td>
								<td>{{item.unFinish}}</td>
								<td>{{item.deliver}}</td>
								<td>{{item.stop}}</td>
								<td>{{item.finishPersents}}</td>
								<td>{{item.averageResponseTime}}</td>
								<td>{{item.averageProcessingTime}}</td>
								<td>{{item.averageWholeTime}}</td>
							</tr>
						</table>
					</vueEasyPrint>
				</div>
			</div>

		</div>
	</div>
</template>
<script>
	import $ from 'jquery'
	import vueEasyPrint from "vue-easy-print"

	export default {
		name: "",
		components: {
			vueEasyPrint,
		},
		data() {
			return {
				tabshow: false,
				types: [{
						name: '按年份',
						id: 1
					},
					{
						name: '按月份',
						id: 2
					},
					{
						name: '按季节',
						id: 3
					},
					{
						name: '按区间',
						id: 4
					},
				],
				quarter: ['第一季度', '第二季度', '第三季度', '第四季度'],
				month: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
				ind: 1,
				ind2: 'all',
				ind3: 'all',
				showzzc: false,
				options_show: false,
				options_show2: false,
				options_show3: false,
				tableData: [],
				startDate: "",
				endDate: "",
				info: {
					searchType: 2,
					startDate: "",
					endDate: "",
					quarter: "",
					engineerId: "",
					departId: "",
					year: "",
					month: "",
				},
				ax: [],
				by: [],
				fin: [],
				unfin: [],
				total: [],
			}
		},
		mounted() {
			this.timeFormate();
			this.getlist();
		},
		watch: {
			roomsearch: {
				handler(newValue, oldValue) {
					if (newValue == '') {

					} else {
						this.getallrooms();
					}
				}
			},
		},
		methods: {
			print() {
				this.$refs.easyPrint.print()
			},
			export2Excel() {
				require.ensure([], () => {
					const {
						export_json_to_excel
					} = require('../../../lib/Export2Excel.js') //刚刚新建Export2Excel.js文件的路径
					const tHeader = ['小组', '完成', '未完成', '转交', '终止', '完成率', '平均响应时长（小时）', '平均处理时长（小时）',
						'平均总时长（小时）'
					] //自定义列名
					const filterVal = ['name', 'finish', 'unFinish', 'deliver', 'termination',
						'finishPersents', 'averageProcessingTime', 'averageResponseTime', 'averageWholeTime'
					] //对应自怼
					const list = this.tableData //table数据
					const data = this.formatJson(filterVal, list);
					const merges = ["A1:I1"]
					export_json_to_excel(tHeader, data, '工程师小组处理情况', merges); //导出文件名称
				})
			},
			formatJson(filterVal, jsonData) {
				// console.info(jsonData)
				return jsonData.map(v => filterVal.map(j => v[j]))
			},

			closeall() {
				this.info.searchType = ''
				this.info.year = ''
				this.info.quarter = ''
				this.info.month = ''
				this.info.startDate = ''
				this.info.endDate = ''
				this.ind = 'all'
			},
			//显示当前时间（年月日时分秒）
			timeFormate(timeStamp) {
				if (timeStamp) {
					let year = new Date(timeStamp).getFullYear();
					let month = new Date(timeStamp).getMonth() + 1
					let date = new Date(timeStamp).getDate()
					return year + "-" + month + "-" + date;
				} else {
					timeStamp = new Date()
					let year = new Date(timeStamp).getFullYear();
					let month = new Date(timeStamp).getMonth() + 1
					// console.log(month)
					this.info.year = year.toString()
					this.ind3 = month - 1
					this.info.month = month
				}
			},
			getlist() {
				if (this.info.year == '' && this.info.searchType == 1) {
					this.$Toast('请选择年份');
					return
				}
				console.log(this.info.year)
				if (this.info.searchType == 1) {
					this.info.month = ''
				}

				if (this.info.searchType == 2) {
					if (this.info.year == '' || this.info.month == '' || this.info.year == null) {
						this.$Toast('请选择年份和月份');
						return
					}
					// return
				}

				if (this.info.searchType == 3) {
					if (this.info.year == '' || this.info.quarter == '' || this.info.year == null) {
						this.$Toast('请选择年份和季度');
						return
					}
					// return
				}
				if (this.info.searchType == 4) {
					if (this.startDate == '' || this.endDate == '' || this.startDate == null || this
						.endDate == null) {
						this.$Toast('请选择开始日期和结束日期');
						return
					}
					console.log(this.timeFormate(this.info.startDate))
					this.info.startDate = this.timeFormate(this.startDate)
					this.info.endDate = this.timeFormate(this.endDate)
					// return
				}

				var that = this
				var param = this.info
				this.$http.post(this.$url + '/statistics/group', this.$qs.stringify(param))
					.then(function(response) {
						if (response.data.code == '1410') {
							that.$Toast(response.data.msg);
							sessionStorage.setItem('token', '')
							that.$router.push('/login')
							return
						}
						that.ax = []
						that.by = []
						that.fin = []
						that.unfin = []
						that.total = []
						that.tableData = response.data.data
						for (var i = 0; i < that.tableData.length; i++) {
							// console.log(that.tableData[i].userName)
							that.ax[i] = that.tableData[i].name ? that.tableData[i].name : ''
							that.fin[i] = that.tableData[i].finish
							that.unfin[i] = that.tableData[i].unFinish
							that.total[i] = that.tableData[i].unFinish + that.tableData[i].finish
							that.by[i] = that.tableData[i].averageProcessingTime
							// that.ax.push(that.tableData[i].userName)
						}
						that.showloading = false
						that.getbar();
						that.gettimelong();
					})
					.catch(function(error) {
						console.log(error);
					});
			},
			formatter(row, column) {
				return row.address;
			},
			gettimelong() {
				var dom = this.$refs.timelong
				var myChart = this.$echarts.init(dom, null, {
					renderer: 'canvas',
					useDirtyRect: false
				});
				var app = {};

				var option;

				option = {
					title: {
						text: "按平均处理时长统计",
						top: 0,
						textStyle: {
							fontSize: 16,
							color: "#626262",
						},
						// padding: 17,
						left: 14
					},
					grid: {
						top: 70,
						left: 20,
						right: 20,
						bottom: 9,
						containLabel: true
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'shadow'
						}
					},
					xAxis: [{
						type: 'category',
						data: this.ax,
						axisLabel: {
							interval: 0,
							rotate: 30
						}
					}],
					yAxis: [{
						type: 'value',
						name: '平均处理时长',
						min: 0,
						nameRotate: this.by.length==0?1:0,
						// max: 72,
						// interval: 12,
						axisLabel: {
							formatter: '{value}小时'
						}

					}],
					series: [{
						barMaxWidth: 70,
						data: this.by,
						type: 'bar',
						itemStyle: {
							color: '#527cff',

						},
					}]
				};

				if (option && typeof option === 'object') {
					myChart.setOption(option);
				}

				window.addEventListener('resize', myChart.resize);
			},
			getbar() {
				var dom = this.$refs.container
				var myChart = this.$echarts.init(dom, null, {
					renderer: 'canvas',
					useDirtyRect: false
				});
				var app = {};

				var option;
				console.log(this.ax)
				option = {
					title: {
						text: "按工单统计",
						top: 0,
						textStyle: {
							fontSize: 16,
							color: "#626262",
						},
						// padding: 17,
						left: 14
					},
					tooltip: {
						trigger: 'axis',
						axisPointer: {
							type: 'shadow'
						}
					},
					legend: {
						right: 20,
						top: 34
					},
					grid: {
						top: 70,
						left: 20,
						right: 20,
						bottom: 9,
						containLabel: true
					},
					xAxis: [{
						type: 'category',
						data: this.ax,
						axisLabel: {
							interval: 0,
							rotate: 30
						}
					}],
					yAxis: [{
						type: 'value',
						name: '工单数量',
						min: 0,
						nameRotate: this.total.length==0?1:0,
						// max: 5,
						// interval: 5,

					}],
					series: [{
							name: '总数',
							type: 'line',
							smooth: true,
							itemStyle: {
								color: '#ff6600'
							},
							data: this.total
						},

						{
							name: '已完成',
							barMaxWidth: 70,
							type: 'bar',
							stack: 'Ad',
							emphasis: {
								focus: 'series'
							},
							itemStyle: {
								color: '#527cff'
							},
							data: this.fin
						},
						{
							name: '未完成',
							barMaxWidth: 70,
							type: 'bar',
							stack: 'Ad',
							emphasis: {
								focus: 'series'
							},
							itemStyle: {
								color: '#91cc75'
							},
							data: this.unfin
						}
					]
				};

				if (option && typeof option === 'object') {
					myChart.setOption(option);
				}

				window.addEventListener('resize', myChart.resize);
			},

			selectit(i) {
				this.ind = i
				this.options_show = false
				this.info.searchType = this.types[this.ind].id
				this.info.month = ''
				this.ind3 = 'all'
				this.showzzc = false

				// this.info.month = this.month[this.ind3]
				// this.ind2 = 'all'
				// this.ind3 = 'all'
			},
			selectit2(i) {
				this.ind2 = i
				this.options_show2 = false
				this.info.quarter = this.ind2 + 1
				this.showzzc = false
			},
			selectit3(i) {
				this.ind3 = i
				this.options_show3 = false
				this.info.month = this.month[this.ind3]
				this.showzzc = false
			},
			// 点击遮罩层
			close() {
				this.options_show = false
				this.options_show2 = false
				this.options_show3 = false
				this.showzzc = false
			},
			changedate(ty) {
				if (this.startDate && this.endDate && this.startDate > this.endDate) {
					this.$Toast('选择的结束日期在开始日期之前');
					if (ty == 1) {
						this.startDate = ''
					} else {
						this.endDate = ''
					}
					// return
				}
			},
		}
	}
</script>
<style scoped>
	/deep/ .el-input__icon {
		line-height: 34px;
	}

	/deep/ .el-table .el-table__cell {
		text-align: center;
	}

	/deep/ .el-table .el-table__cell:nth-of-type(1) {
		text-align: left;
	}

	/deep/ .el-table .el-table__cell:nth-of-type(2) {
		text-align: left;
	}

	.table_top {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-top: 23px;
		margin-bottom: 11px;
	}

	.table_title {
		font-size: 16px;
		font-weight: bold;
		color: #626262;
	}

	.top_btns {
		display: flex;
	}

	.top_btns div {
		font-size: 14px;
		color: #FFFFFF;
		text-align: center;
		line-height: 28px;
		border-radius: 3px;
		width: 63px;
		height: 28px;
		margin-left: 13px;
		cursor: pointer;
	}

	.print {
		background: #32B16C
	}

	.export {
		background: #5BC0DE;
	}

	.bg {
		background: #f6f6f6;
		min-height: 100vh;
	}

	.box_top_left {
		padding-top: 17px;
		height: 370px;
		width: 49%;
		background-color: #fff;
	}

	.box_top {
		display: flex;
		justify-content: space-between;
		margin-top: 17px;
	}

	.box {
		-ms-overflow-style: none !important;
	}

	.box::-webkit-scrollbar {
		display: none;
	}

	.box {
		width: 100%;

		overflow-y: scroll;
		height: calc(100vh - 116px);
	}

	/deep/ .el-input--prefix .el-input__inner {
		height: 34px;
		padding: 0 8px;
	}

	.input-cont:hover .clears {
		display: block;
	}

	.clears {
		width: 10px;
		position: absolute;
		right: 35px;
		top: 14px;
		display: none;
		cursor: pointer;
	}

	.el-input__inner {
		width: 114px;
		height: 34px;
		background: #F6F6F6;
		border: 1px solid #CCCCCC;
		box-shadow: 0px 2px 0px 0px #EEEEEE;
		border-radius: 5px;
	}
</style>
